<?php
/* creates an html element, like in js */
/* REFERENCIA: http://davidwalsh.name/create-html-elements-php-htmlelement-class */
class html_element {
	/* vars */
	var $type;
	var $attributes;
	var $self_closers;
	
	/* constructor */
	function html_element($type, $self_closers = array('input','img','hr','br','meta','link')) {
		$this->type = strtolower($type);
		$this->self_closers = $self_closers;
		$this->attributes = array('innerHTML'=>'');
	}
	
	/* get */
	function get($attribute) {
		return $this->attributes[$attribute];
	}
	
	/* set -- array or key,value */
	function set($attribute,$value = '') {
		if(!is_array($attribute)) {
			$this->attributes[$attribute] = utf8_decode($value);
		}
		else {
			foreach ($attribute as $key => $value)
				$attribute[$key] = utf8_decode($value);
			$this->attributes = array_merge($this->attributes,$attribute);
		}
	}
	
	/* remove an attribute */
	function remove($att) {
		if(isset($this->attributes[$att])) {
			unset($this->attributes[$att]);
		}
	}
	
	/* clear */
	function clear() {
		$this->attributes = array();
	}
	
	/* inject */
	function inject($object) {
		if(@get_class($object) == __class__) {
			$this->attributes['innerHTML'].= $object->build();
		}
	}
	
	/* build */
	function build() {
		//start
		$build = '<'.$this->type;
		
		//add attributes
		if(count($this->attributes)) {
			foreach($this->attributes as $key=>$value)
				if($key != 'innerHTML') { $build.= ' '.$key.'="'.$value.'"'; }
		}
		
		//closing
		if(!in_array($this->type,$this->self_closers)) {
			$build.= '>'.$this->attributes['innerHTML'].'</'.$this->type.'>
';
		} else {
			$build.= ' />
';
		}
		
		//return it
		return $build;
	}
	
	/* spit it out */
	function output()
	{
		echo $this->build();
	}
}
?>

